<?php
include_once("../includes/page_utf_class.php");
include("../lang/cn/company_type_config.php");
include_once("../module/micro_shop/includes/plugin_micro_shop_class.php");

$microShop = new MicroShop();

if($_GET['act'] && $_GET['act']=='ajax_get_tarento'){
	$list = array();
	if ($_POST['id'] > 0) {
		$sql = "select member_id from ".TARENTO." where id={$_POST['id']}";
		$db->query($sql);
		$mid = $db->fetchField('member_id');
		$list = $microShop->get_tarento_list($mid);
	} else {
		if($_GET['member_id']>0){
			$list = $microShop->get_tarento_list($_GET['member_id'],TRUE);
		}else{
			$list = $microShop->get_tarento_list();
		}
	}
	foreach ($list as $k=>$v)
	{
		$salesInfo = $microShop->get_tarento_current_month_sales($v['member_id']);
		$list[$k]['member_num'] = $microShop->get_tarento_group_total_num($v['member_id']);
		$list[$k]['business_num'] = $microShop->get_tarento_business_total_num($v['member_id']);
		$list[$k]['self_sales'] = $salesInfo['self_sales']?$salesInfo['self_sales']:0;
		$list[$k]['group_sales'] = $salesInfo['group_sales']?$salesInfo['group_sales']:0;
		$list[$k]['state'] = $list[$k]['member_num']>1?'closed':'open';
		$list[$k]['name'] = empty($v['name'])?'匿名用户':$v['name'];
		if($_POST['id'] > 0){
			$list[$k]['parentId'] = $_POST['id'];
		}
		//店主微信二维码
		if(!empty($v['personal_qrcode'])){
		    $list[$k]['personal_qrcode'] = "<img class='personal_qrcode' code-src='".$v['personal_qrcode']."' style='width:30px;pidding-left:10px;' src='../image/admin/saveErweiPop.png' />";
		}
		$list[$k]['invite_code'] = $v['invite_code'];
		
		//店铺等级
		$level_str = "吃货";
		$level_img_num = 3;
		$padding = 0;
		if($v['level']==1){
		    $level_str = "首席";
		    $level_img_num = 1;
		    $padding = 0;
		}elseif ($v['level']==2){
		    $level_str = "红人";
		    $level_img_num = 2;
		    $padding = 0;
		}
		$list[$k]['shop_level'] = "<span style='padding-left:".$padding."px;'><img style='height:55px;' src='../image/admin/micro_shop_".$level_img_num.".png' width='30' />".$level_str."</span>";
		$list[$k]['view'] = "<a href='?m=micro_shop&s=shop_detail.php&id=".$v['id']."'>查看</a>";
		if($v['level']>=2){
		    $list[$k]['view'] .= "&nbsp;<a class='a-opear' data-type='shop_upgrade' data-param=\"{'mid':'".$v['member_id']."'}\" href='javascript:void(0);'><img src='../image/admin/set.png' /></a>";
		}
		//直接推荐人和间接推荐人店名
		$direct_shop_name = "";
		$indirect_shop_name = "";
		if($v['direct_rec_mid']>0){
		    $direct_shop_name = "店铺ID：".$v['direct_rec_mid']."<br/>店铺名称：".$v['direct_shop_name'];
		}
		if($v['indirect_rec_mid']>0){
		    $indirect_shop_name = "店铺ID：".$v['indirect_rec_mid']."<br/>店铺名称：".$v['indirect_shop_name'];
		}
		$list[$k]['direct_rec_mem'] = $direct_shop_name;
		$list[$k]['indirect_rec_mem'] = $indirect_shop_name;
		//上级店铺
		$higherShopId = 0;
		$list[$k]['higher_shop'] = "";
		$tmpRelations = str_replace(','.$v['member_id'].',', '', ','.$v['relations']);
		if(!empty($tmpRelations)){
		    $higherShopId = strpos($tmpRelations, ',')===false ? $tmpRelations : strrchr($tmpRelations, ',');
		    $higherShopId = trim($higherShopId,',');
		}
		if($higherShopId>0){
    		$higherShopInfo = $microShop->get_shop_info($higherShopId);
		    if(!empty($higherShopInfo)){
		        $list[$k]['higher_shop'] = "店铺ID：".$higherShopId."<br/>店铺名称：".$higherShopInfo['name'];
		    }
		}
	}
	
	echo json_encode($list);exit;
}
if($_GET['act'] && $_GET['act']=='saveInfo'){//保存
	if(!empty($_POST['data'])){
		if($_POST['data']['id']>0){
			$sql = "update ".TARENTO." set lowest_rate='{$_POST['data']['lowest_rate']}' where id={$_POST['data']['id']}";
			$db->query($sql);
			$res = array('code'=>0,'msg'=>'修改成功!');
		}else{
			$res = array('code'=>1,'msg'=>'要修改的数据不存在!');
		}
	}else{
		$res = array('code'=>2,'msg'=>'修改数据为空!');
	}
	echo json_encode($res);exit;
}

//店铺数量
$shopNumInfo[1] = $microShop->getShopNum('1');
$shopNumInfo[2] = $microShop->getShopNum('2');
$shopNumInfo[3] = $microShop->getShopNum('3');

$tpl->assign("list",json_encode($de['list']));
$tpl->assign("config",$config);
$tpl->assign("shopNumInfo",$shopNumInfo);
$tpl->display("shop_list.htm");
?>